﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;

namespace stata
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string sciezka;
        public static Operations op;
        List<String> polist = new List<string>();

        public MainWindow()
        {
            InitializeComponent();
            op = new Operations();
         
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem m = sender as MenuItem;
            if (m.Header.ToString() == "Wczytaj")
            {
                // Create OpenFileDialog
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

                // Set filter for file extension and default file extension


                // Display OpenFileDialog by calling ShowDialog method
                Nullable<bool> result = dlg.ShowDialog();

                // Get the selected file name and display in a TextBox
                if (result == true)
                {
                    op.dt.Rows.Clear();
                    op.dt.Columns.Clear();
                    dataGrid1.ItemsSource = null;
                    // Open document
                    string filename = dlg.FileName;
                    sciezka = filename;
                    nazwy n = new nazwy(sciezka, op);
                    n.ShowDialog();
                    dataGrid1.ItemsSource = op.dt.DefaultView;
                    DataRowView r = (DataRowView)dataGrid1.Items[0];
                    dataGrid1.Items.Refresh();

                }

            }
            else if (m.Header.ToString() == "Wykresy")
            {
                Wykresy dys = new Wykresy(op);
                dys.ShowDialog();

            }
            else if (m.Header.ToString() == "srednia arytmetyczna")
            {
                DataTable pomdt = new DataTable();
                pomdt = op.dt;
                for (int i = 0; i < op.dt.Columns.Count - 1; i++)
                {
                    for (int j = 0; j < op.dt.Rows.Count; j++)
                    {
                        if (op.dt.Rows[j].Field<string>(i) == "?")
                        {
                            pomdt.Rows[j].SetField<string>(i, op.Abs(op.dt.Columns.Count - 1, i, j).ToString());
                        }
                    }
                }
                dataGrid1.ItemsSource = pomdt.DefaultView;
                dataGrid1.Items.Refresh();
            }
            else if (m.Header.ToString() == "Rozkład SVD")
            {
                DataTable pomdt = op.SVD(op.dt);
                dataGrid1.ItemsSource = pomdt.DefaultView;
                dataGrid1.Items.Refresh();
            }
            else if (m.Header.ToString() == "K najblizszych sasiadow")
            {
                
                int k=0;
                DataTable pomdt = new DataTable();
                KNN knn = new KNN();
                knn.ShowDialog();
                k = knn.k;
                pomdt = op.dt;
                
                for (int i = 0; i < op.dt.Columns.Count - 1; i++)
                {
                    for (int j = 0; j < op.dt.Rows.Count; j++)
                    {
                        List<String> nowy = new List<string>();
                        if (op.dt.Rows[j].Field<string>(i) == "?")
                        {
                            for (int jj = 0; jj < op.dt.Columns.Count; jj++)
                            {
                                try
                                {
                                    String wiersz = op.dt.Rows[j].Field<String>(jj);
                                    wiersz = wiersz.Replace('.', ',');
                                    float w = float.Parse(wiersz);

                                    nowy.Add(wiersz);
                                }
                                catch (Exception ee)
                                {

                                }
                            }
                            pomdt.Rows[j].SetField<string>(i, op.klasyfikacjaEuklides(nowy,i,k,j));
                        }
                    }
                }
                dataGrid1.ItemsSource = pomdt.DefaultView;
                dataGrid1.Items.Refresh();


            }
            else if (m.Header.ToString() == "Zapisz")
            {
                DataTable t = op.dt;
                // Create OpenFileDialog
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

                // Set filter for file extension and default file extension
                dlg.DefaultExt = ".txt";
                dlg.Filter = "Text documents (.txt)|*.txt";

                // Display OpenFileDialog by calling ShowDialog method
                Nullable<bool> result = dlg.ShowDialog();

                // Get the selected file name and display in a TextBox
                if (result == true)
                {
                    byte[] buffer;

                    string filename = dlg.FileName;
                    sciezka = filename;
                    FileStream fileStream = new FileStream(sciezka, FileMode.Create, FileAccess.Write);



                    int length = (int)fileStream.Length;  // get file length



                    int count = 0;                            // actual number of bytes read

                    int sum = 0;                          // total number of bytes read

                    int readthis = 1;

                    for (int z = 0; z < t.Columns.Count; z++)
                    {
                        String s = t.Columns[z].ColumnName;
                        buffer = new byte[s.Length + 1];
                        char[] c = s.ToCharArray();
                        for (int k = 0; k < c.Length; k++)
                        {
                            buffer[k] = (byte)c[k];
                            fileStream.WriteByte((byte)c[k]);

                        }

                        fileStream.WriteByte(9);
                    }

                    fileStream.WriteByte((byte)10);
                    for (int i = 0; i < t.Rows.Count; i++)
                    {
                        for (int j = 0; j < t.Columns.Count; j++)
                        {
                            String s = t.Rows[i].Field<String>(j);
                            buffer = new byte[s.Length + 1];
                            char[] c = s.ToCharArray();
                            for (int k = 0; k < c.Length; k++)
                            {
                                fileStream.WriteByte((byte)c[k]);
                            }
                            fileStream.WriteByte((byte)'\t');


                        }


                        fileStream.WriteByte((byte)10);
                    }


                }
            }


        }




    }
}

